package randomdata;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.Path;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.UnknownHostException;
import java.util.Random;

public class CreateData {
	static InputStream inputStream;
	
	static final String ipaddr = GetIp.getServerIp();
	
	static long convert(String batch) {
		
		long num = Integer.parseInt(batch.substring(0, batch.length() - 1));
		switch (batch.substring(batch.length() - 1)) {
		case "t":
		case "T":
			return num * 1024 * 1024 * 1024 * 1024;
		case "g":
		case "G":
			return num * 1024 * 1024 * 1024;
		case "m":
		case "M":
			return num * 1024 * 1024;
		case "k":
		case "K":
			return num * 1024;
		case "b":
		case "B":
			return num;
		default:
			throw new IllegalArgumentException("wrong data size");
		}
		
	}
	
	static String createrecord() throws UnknownHostException {
		
        String randomcode = createrandomcode().toString();

        String record = ipaddr + randomcode + System.currentTimeMillis();

        return record;
	}
	
	//���ݴ����̣߳�Ŀǰֻ֧�ֵ��߳�
	static void copythread(String dst, Long batch_int) throws IOException {
//        Configuration conf = new Configuration();
//        String dist =  dst + createrecord();
//        java.io.FileSystem fs = java.io.FileSystem.get(URI.create(dist), conf);
//        FSDataOutputStream out = fs.create(new Path(dist));
//
////        String randomcode;
//        int dataNum = 100;
//
//        long batchsize =  batch_int / FormatData.getARecord().length() / dataNum;
//        System.out.println("batchsize:" + FormatData.getARecord().length() + ", batchs:" + batchsize);
//        long second =System.currentTimeMillis();
//
//        for (int i = 0; i < batchsize; i++) {
////        	randomcode = createrandomcode().toString();
//        	StringBuffer record = new StringBuffer();
//        	for (int j = 0; j < dataNum; j ++){
//				record.append(FormatData.getARecord()).append("\n");
//			}
//        	out.write(record.toString().getBytes("UTF-8"));
//        }
//        out.close();
//		fs.close();
//        System.out.println("total spent:" + (System.currentTimeMillis()-second)/1000 + " second, dist:" + dist);
	}
	
	//���������
	static Integer createrandomcode() {
        Random random = new Random();
        Integer ret = 0;
        for (int i = 0; i < 3; i++) {
        	ret += random.nextInt(9) + 1;
        	if (i < 2)
        		ret*=10;
        }
        return ret;
	}
	
    public static void main(String[] args) throws IOException, InterruptedException {
    	String dir = args[0];
    	dir = dir.endsWith("/")?dir:dir + "/";
    	
    	String batch = args[1];
        final long batch_int = convert(batch);
        

        final String dirs = dir;
        Thread thread1 = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					copythread(dirs, batch_int);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
        
        thread1.start();
//        thread1.join();
//        IOUtils.copyBytes(createData.inputStream, out, 4096, true);
    }
}
